<?php

include FCPATH . 'application/controllers/backend/backend.php';

class Setting extends Backend {

    public function __construct() {
        parent::__construct();
        $this->load->model('setting_model', 'model');
    }

    public function home() {
        $settings = $this->model->records();

        $data = array();
        foreach ($settings as $setting) {
            $data[$setting->key] = json_decode($setting->value);
        }

        $this->load_javascripts('backend_setting_home');

        $this->rend('backend/setting_home_view', $data);
    }

    public function find() {
        $id = intval($this->input->get('id'));

        if (!($data = $this->model->find($id))) {
            $this->json_back('XT002', '该条记录不存在');
        }

        $this->json_back('XT001', '', $data);
    }

    public function records() {
        $key = trim($this->input->get('key'));

        $conditions = array();
        if (!empty($key)) {
            $conditions[] = array('key', $key);
        }

        $this->json_back('XT001', '', array(
            'total' => $this->model->total(),
            'records' => $this->model->records(0, 0, $conditions)
        ));
    }

    public function insert() {
        $this->load->model('album_model');
        $data = array(
            'key' => trim($this->input->post('key')),
            'value' => $this->input->post('value'),
        );

        if (empty($data['key'])) {
            $this->json_back('XT002', '键不能为空');
        }

        if (empty($data['value'])) {
            $this->json_back('XT002', '值不能为空');
        }

        if ($this->model->insert($data)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

    public function update_sub_menu_content() {
        $id = intval($this->input->post('id'));

        $zh = $this->input->post('zh');
        $en = $this->input->post('en');

        $data = array(
            'value' => json_encode(array('zh' => $zh, 'en' => $en))
        );

        if (empty($data['value'])) {
            $this->json_back('XT002', '值不能为空');
        }

        if ($this->model->update($id, $data)) {
            $this->json_back('XT001', '更新成功');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));

        $data = array(
            'value' => $this->input->post('value')
        );

        if ($this->model->update($id, $data)) {
            $this->json_back('XT001', '更新成功');
        } else {
            $this->json_back('XT002', '数据库或查询语句有误');
        }
    }

    public function update_by_key() {
        $key = trim($this->input->post('key'));
        
        $data = array(
            'value' => $this->input->post('value')
        );
        
        if ($this->model->update_by_key($key, $data)) {
            $this->json_back('XT001', '更新成功');
        } else {
            $this->json_back('XT002', '数据库或查询语句有误');
        }
    }

}

